package com.bistu.sim.xwy.news.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bistu.sim.xwy.news.R;
import com.bistu.sim.xwy.news.domain.Goods;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

public class GoodsAdapter extends BaseAdapter {
    private Context context;
    private List<Goods> list;

    public GoodsAdapter(Context context, List<Goods> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder viewHolder;

        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(R.layout.goods_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.goods_image = view.findViewById(R.id.goods_image);
            viewHolder.goods_name = view.findViewById(R.id.goods_name);
            viewHolder.goods_type = view.findViewById(R.id.goods_type);
            viewHolder.goods_desc = view.findViewById(R.id.goods_desc);
            viewHolder.goods_price = view.findViewById(R.id.goods_price);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        Goods item = list.get(position);
        viewHolder.goods_name.setText(item.getGoods_name());
        viewHolder.goods_type.setText(item.getGoods_type());
        viewHolder.goods_desc.setText(item.getGoods_content());
        viewHolder.goods_price.setText(item.getGoods_priece());

        RequestOptions requestOptions = new RequestOptions()
                .placeholder(R.drawable.saler_load);

        String imageUrl = context.getString(R.string.saler_url) + item.getGoods_imageaddress();
        Glide.with(context).load(imageUrl)
                .apply(requestOptions)
                .into(viewHolder.goods_image);

        return view;
    }

    static class ViewHolder {
        ImageView goods_image;
        TextView goods_name;
        TextView goods_type;
        TextView goods_desc;
        TextView goods_price;
    }
}